import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bubble/flutter_bubble.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  bool _hasPermission = false;
  bool _isShowing = false;

  @override
  void initState() {
    super.initState();
    initPlatformState();
    checkPermission();
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await FlutterBubble.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future<void> checkPermission() async {
    try {
      final bool hasPermission = await FlutterBubble.checkPermission();
      setState(() {
        _hasPermission = hasPermission;
      });
    } on PlatformException {
      setState(() {
        _hasPermission = false;
      });
    }
  }

  Future<void> showBubble() async {
    try {
      final bool result = await FlutterBubble.showBubble();
      setState(() {
        _isShowing = result;
      });
    } on PlatformException {
      setState(() {
        _isShowing = false;
      });
    }
  }

  Future<void> hideBubble() async {
    try {
      final bool result = await FlutterBubble.hideBubble();
      setState(() {
        _isShowing = !result;
      });
    } on PlatformException {
      // Handle error
    }
  }

  Future<void> requestPermission() async {
    await FlutterBubble.requestPermission();
    // 重新检查权限
    await Future.delayed(Duration(seconds: 1));
    await checkPermission();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('悬浮球示例'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('运行平台: $_platformVersion\n'),
              SizedBox(height: 20),
              Text('悬浮窗权限: ${_hasPermission ? "已授权" : "未授权"}'),
              SizedBox(height: 20),
              if (!_hasPermission)
                ElevatedButton(
                  onPressed: requestPermission,
                  child: Text('请求悬浮窗权限'),
                ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: _hasPermission && !_isShowing ? showBubble : null,
                child: Text('显示悬浮球'),
              ),
              SizedBox(height: 10),
              ElevatedButton(
                onPressed: _isShowing ? hideBubble : null,
                child: Text('隐藏悬浮球'),
              ),
              SizedBox(height: 20),
              Text('操作说明:'),
              Text('1. 点击"请求悬浮窗权限"获取权限'),
              Text('2. 点击"显示悬浮球"显示悬浮球'),
              Text('3. APP在前台时悬浮球会自动隐藏'),
              Text('4. 将APP切换到后台时悬浮球会自动显示'),
              Text('5. 点击悬浮球可将APP从后台拉起到前台'),
            ],
          ),
        ),
      ),
    );
  }
}
